package com.jdkreader.concurrency.concurrent.priority;

import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

public class PrioritizedTaskProducer implements Runnable{

    private Random random=new Random(47);

    private Queue<Runnable> queue;

    private ExecutorService exec;

    public PrioritizedTaskProducer(Queue<Runnable> q, ExecutorService e) {
        this.queue = q;
        this.exec = e;
    }

    public void run(){
//        for(int i =0;i < 20;i++){
//            queue.add(new PrioritizedTask(random.nextInt(10)));
//            Thread.yield();
//        }
        try{
            //添加高优先级任务
            for(int i=0;i<20;i++){
                TimeUnit.MILLISECONDS.sleep(250);
                queue.add(new PrioritizedTask(random.nextInt(10)));
            }

            for(int i=0;i<10;i++){
                TimeUnit.MILLISECONDS.sleep(250);
                queue.add(new PrioritizedTask(i));
            }
            queue.add(new EndSentinel(exec));

        }catch(InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("Finished PrioritizedTaskProduce");
    }


}
